package pub.dsb.example.thread;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * https://leetcode-cn.com/problems/print-zero-even-odd/
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2020/12/31 9:59
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2020/12/31 9:59
 * @modify reason: {方法名}:{原因}
 * ...
 * @since V1.0.0
 */
public class LC1116Test {

    public static void main(String[] args) {
        ZeroEvenOdd zeroEvenOdd = new ZeroEvenOdd(2);
        new Thread(() -> {
            try {
                zeroEvenOdd.zero();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T-A").start();
        new Thread(() -> {
            try {
                zeroEvenOdd.even();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T-B").start();
        new Thread(() -> {
            try {
                zeroEvenOdd.odd();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "T-C").start();
    }
}

class ZeroEvenOdd {
    private AtomicInteger counter = new AtomicInteger(0);
    private volatile boolean printZero = true;
    private volatile boolean printEven = false;
    private volatile boolean printOdd = false;
    private Lock lock = new ReentrantLock();
    private Condition zeroCondition = lock.newCondition();
    private Condition evenCondition = lock.newCondition();
    private Condition oddCondition = lock.newCondition();
    private int n;

    public ZeroEvenOdd(int n) {
        this.n = n;
    }

    // printNumber.accept(x) outputs "x", where x is an integer.
    public void zero() throws InterruptedException {
        for (int i = 0; i < n; i++) {
            lock.lock();
            try {
                while (!printZero) {
                    zeroCondition.await();
                }
                print("0");
                counter.addAndGet(1);
                if (counter.get() % 2 == 0) {
                    printEven = true;
                    evenCondition.signal();
                } else {
                    printOdd = true;
                    oddCondition.signal();
                }
                printZero=false;
            } finally {
                lock.unlock();
            }
        }
    }

    private void print(Object s) {
        System.out.print(s);
    }

    /**
     * 输出偶数
     *
     * @throws InterruptedException
     */
    public void even() throws InterruptedException {
        for (int i = 0; i < n/ 2; i++) {
            lock.lock();
            try {
                while (!printEven) {
                    evenCondition.await();
                }
                print(counter.get());
                zeroCondition.signal();
                printEven = false;
                printZero = true;
            } finally {
                lock.unlock();
            }
        }
    }

    /**
     * 奇数
     *
     * @throws InterruptedException
     */
    public void odd() throws InterruptedException {
        for (int i = 0; i < (n + 1) / 2; i++) {
            lock.lock();
            try {
                while (!printOdd) {
                    oddCondition.await();
                }
                print(counter.get());
                zeroCondition.signal();
                printOdd = false;
                printZero = true;
            } finally {
                lock.unlock();
            }
        }
    }
}